 // gjw/pages/doubao/doubao.js

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
		fullscreen:0,
    tab_menu:['宝贝详情','宝贝评论'],
    activeIndex:0,
    index:0,
    content:[
      { photo: 'https://img01.sogoucdn.com/app/a/100520093/e18d20c94006dfe0-feec70b0eb485633-e7e784240dab719c6e65117bcc3abd7a.jpg', name: '林志玲', date: '08-01', txt: '这里是评论内容这里是评论内容这里是评论内容' },
      { photo: 'https://img01.sogoucdn.com/app/a/100520093/e18d20c94006dfe0-feec70b0eb485633-e7e784240dab719c6e65117bcc3abd7a.jpg', name: '林志玲', date: '08-01', txt:'这里是评论内容这里是评论内容这里是评论内容'},
    ],
    noData:false,
    pl_list:true,
    box:true,
    zan:'../images/zan.png',
    zanTxt:'我要点赞',
    css:'goods_zan',
    gz: 0,
    tag: '',
    star: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
		console.log(options)
    that.identy()    
    options.time = options.time.slice(0,11)
    that.setData({
      comdetail: options,
      agree: options.agree,
      view: options.view,
			siteUrl: wx.getStorageSync('siteUrl')
    })
    that.getData()
    that.view()
    that.dz()
    that.get_pl()
    that.gz()    
  },

  // 判断身份
  identy: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    
    app.util.request({
      url: 'entry/wxapp/Iscustomers',
      data: { openid: openid },
      success: function (res) {
        
        if (res.data.code == 200 || res.data.code == 204) {    //商家审核通过  商家
          that.setData({
            zt: 1
          })
        } else{    //商家审核未通过  用户
          that.setData({
            zt: 0
          })
        }
      }
    })
  },

  // 判断是否关注
  gz: function (e) {
    var that = this
    var gz = that.data.gz
    var openid = wx.getStorageSync('openid')
    var phone = that.data.comdetail.phone
    
    if (gz == 0) {
      app.util.request({
        url: 'entry/wxapp/Islovestore',
        data: {
          openid: openid, phone: phone
        },
        success: function (res) {
          
          if(res.data.code==500){
            that.setData({
              gz: 0
            })
          } else if (res.data.code == 200)
              that.setData({
                gz: 1
              })
        }
      })
    } 
  },



  // 浏览量
  view:function(e){
    var that=this
    var openid = wx.getStorageSync('openid')
    var g_id = that.data.comdetail.id 
    app.util.request({
      url: 'entry/wxapp/Dbview',
      data: { openid: openid, id: g_id },
      success:function(res){
        
        if(res.data.code==200){
          that.setData({
            view:res.data.res
          })
        }
      }
    })
  },




  // 判断是否已经点赞
  dz:function(e){
    var that=this
    var openid = wx.getStorageSync('openid')
    var g_id = that.data.comdetail.id 
    
    
    app.util.request({
      url:'entry/wxapp/Dbagreedetail',
      data:{openid:openid,id:g_id},
      success:function(res){
        
        if(res.data.code==200){
          that.setData({
            zan: '../images/zan_on.png',
            zanTxt: '已点赞',
            css: 'on_zan',
          })
        } 
      }
    })
  },

  // 获取评论
  get_pl:function(e){
    var that=this
    var show = false
    var hide=true
    var g_id = that.data.comdetail.id 
    app.util.request({
      url: 'entry/wxapp/Dbassessget',
      data: {p_id: g_id },
      success: function (res) {
        
        if(res.data.code==200){
          res.data.res.reverse()
          that.setData({
            noData:hide,
            pl_list:show,
            contxt:res.data.res
          })
        }else if(res.data.code==500){
          that.setData({
            pl_list:hide,
            noData: show
          })
        }
      }
    })   
  },

  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.dataset.offsetLeft,
      activeIndex: e.currentTarget.dataset.index
    })
  },




  // 点赞
  clickZan:function(e){
    var that=this
    var openid = wx.getStorageSync('openid')
    var g_id = that.data.comdetail.id 
    var s_agree =that.data.agree
    
    
    app.util.request({
      url: 'entry/wxapp/Dbagreedetail',
      data: { openid: openid, id: g_id },
      success: function (res) {
        
        if (res.data.code == 200) {
          wx.showToast({
            title: '您已点过赞咯',
            icon:'none'
          })
          that.setData({
            zan: '../images/zan_on.png',
            zanTxt: '已点赞',
            css: 'on_zan',
          })
        }
        else if (res.data.code == 500) {
          app.util.request({
            url: 'entry/wxapp/Dbagree',
            data: { id: g_id, openid: openid },
            success: function (res) {
              
              if (res.data.code == 200) {
                wx.showToast({
                  title: '点赞成功',
                  icon: 'success',
                })
                that.setData({
                  zan: '../images/zan_on.png',
                  zanTxt: '已点赞',
                  css: 'on_zan',
                  agree: res.data.res
                })
              }
              //  else if (res.data.res == 201) {
              //   wx.showToast({
              //     title: '你已点赞，不能重复操作',
              //     icon: '',
              //   })
              // }
            }
          })
        }
      }
    })
    
    
  },
  

  // 写评论
  go_pl:function(e){
    var show=false
    this.setData({
      box:show
    })
  },
  // 评论提交
  formSubmit:function(e){
    var that=this
    var hide=true
    var formid = e.detail.formId
    var content = e.detail.value.content
    var g_id = that.data.comdetail.id
    var openid = wx.getStorageSync('openid')
    
    if(content==""){
      wx.showToast({
        title: '请填写评论内容',
        icon:'none',
      })
    }else{
      app.util.request({
        url:'entry/wxapp/Dbassess',
        data:{
          openid:openid,formid:formid,content:content,p_id:g_id
        },
        success:function(res){
          
          if(res.data.code==200){
            wx.showToast({
              title: '评论成功',
              icon:'none'
            })
            that.setData({    //更新评论列表
              box:hide, 
            })
            that.get_pl()
          }else{
            wx.showToast({
              title: '评论失败',
              icon: 'none'
            })
            
          }
        }
      })
    }
   
  },


  // 关注
  follow:function(e){
    var that=this
    var gz=that.data.gz
    var openid = wx.getStorageSync('openid')
    var phone = that.data.comdetail.phone
    
    if(gz==0){
      app.util.request({
        url: 'entry/wxapp/Lovestore',
        data: {
          openid: openid, phone:phone
        },
        success: function (res) {
          
          that.setData({
            gz: 1
          })
          wx.showToast({
            title: '关注成功',
            icon: 'success'
          })
        }
      })  
    }else{
      wx.showToast({
        title: '您已经关注该店铺了',
        icon:'none'
      })
    }
  },
  blur:function(e){
    var hide= true    
    this.setData({
      box: hide
    })
  },  
  
  // 店铺评分
  getData: function (e) {
    var that = this
    var phone = that.data.comdetail.phone
    app.util.request({
      url: 'entry/wxapp/Lovestoreinfo',
      data: {
        phone: phone
      },
      success: function (res) {
        that.setData({
          tag: res.data
        })
        var num = that.data.tag.star
        var star = []
        for (var i = 0; i < num; i++) {
          star.push('../images/star_on.png')
        }
        that.setData({
          star: star
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.videoContext = wx.createVideoContext('myVideo')
  },
	play: function (e) {
		// var that=this
		// if (that.data.fullscreen==0){
			this.videoContext.requestFullScreen()
		// 	that.setData({
		// 		fullscreen:1
		// 	})
		// } else if(that.data.fullscreen == 1){
		// 	this.videoContext.exitFullScreen()
		// 	that.setData({
		// 		fullscreen: 1
		// 	})
		// }
	},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})